<?php


/**
 * 会员管理
 * Enter description here ...
 * @author Admin
 *
 */
class MemberAction extends CommonAction{
	
	//会员
	public $user = null;
	
	
	/**
	 * 初始化方法
	 * @see CommonAction::_initialize()
	 */
	protected function _initialize() {
		$user = A('User');
		$info = $user->_islogin();  //用户登录情况
		
		if ($info == false)  //未登录
			//$this->redirect('Index/index');   //跳转首页
			
			$this->redirect('User/register');   //跳转到登录页面
		
		
			
		$this->user = $info;
		$this->assign('user',$this->user);
		
		
		$auction = D('Auction');   //竞拍
		$auction->endState();  
		
		
		parent::_initialize();   //更新竞拍和订单状态
	}
	
	
	protected function page($count,$num = 10,$data=null,$location='ajax_list'){
    	import("@.ORG.AjaxPage"); //导入分页类
		$p = new AjaxPage ($count,$num,$location);
		$limit = $p->firstRow.','.$p->listRows;
		$p->setConfig('theme','%first% %upPage% %prePage% %linkPage% %nextPage% %downPage% %end%');
		//分页条件参数
		if (!empty($data)){
			foreach($data as $key=>$val) {
				$p->parameter   .=   "$key=".$val."&";
			}
		}
		
		$page = $p->show ();
		$this->assign('page',$page);
		return $limit;
    }
    
    
    
	/**
	 * 会员登录首页
	 * Enter description here ...
	 */
	public function index(){
		
		$void = $_GET['void'];   //模块：eg：会员有短消息进入 void/ mb_message
		
		$this->assign('void',$void);
		
		$this->display();
	}
	
	
	/**
	 * 首页保存留言
	 * 保存留言
	 * Enter description here ...
	 */
	public function saveMessage(){
		$data = Init_GP(array('message','title'));

		if (empty($data['message']) || empty($data['title']) || $data['message'] == "想拍什么？写下来！"){
			$json = 0;
		}else {
			$message = D('Message');
			$messagedata = array(
				'uid' => $this->user['id'],
				'name'=>$this->user['name'],
				'message'=>$data['message'],
				'ip'=>get_client_ip(),
				'title'=>$data['title'],
				'addtime'=>time(),
			);
			$info = $message->add($messagedata);
			if ($info){
				$json = 1;//成功
			}else {
				$json = 0;//失败
			}
		}
		echo $json;
	}
	
	
	
	
	
	 /**
     * 注册用户打卡获取虚拟竞标币
     * Enter description here ...
     */
    //TODO:........
	public function ajaxDoExpoint(){
		$punch = D('Punch');
		$config = C('SYSCONFIG');
		
		//TODO:..用户验证状态何处控制--手机认证
		if ($this->user['state'] == 1){   //用户验证状态
			if ($punch->isPunch($this->user['id'])){    //已经打卡
				$json = 2;
			}else {
				if($punch->punching($this->user['id'],$this->user['name'])){
					//....用户打卡成功，记录交易记录
					
					
					$transaction = D('Transaction');     //....交易记录
					$transactiondata = array(
						'uid'=>$this->user['id'],
						'name'=>$this->user['name'],
						'payment'=>'打卡',
						'remark'=>"用户打卡领取{$config['punch_pointb']}{$config['pointb_name']}点"  ,
					
						'money'=>$config['punch_pointb'],
						'addtime'=>time(),
						'type'=>"pointb",
						'source'=>'3',
						'robot'=>'0'
					
					
					);
					$transaction->insert($transactiondata);
					
					
					$user = D('User');   //..修改用户虚拟币数量
					$user->setInc('pointb',"id = {$this->user['id']}",$config['punch_pointb']);
					
					
					$json = 1;
				}else{
					$json = 0;
				}
			}
		}else{
			$json = 3;
		}
		echo $json;
	}
    
	
	
	
	
	
	
	
	
	/**
	 * 个人管理首页
	 * Enter description here ...
	 */
	public function mb_index(){
		$this->display();
	}
	
	
	
	/**
	 * 保存优先使用点设置
	 * Enter description here ...
	 */
	public function saveSetting(){
		$data = Init_GP(array('setting'));
		$priority = GetNum($data['setting']);
		$json = null;
		if (!empty($priority)){
			$user = D('User');
			
			$map['id'] = array('eq',$this->user['id']);
			$info = $user->where($map)->setField('priority',$priority);
			
			if (!empty($info)){
				$json =  1;
			}
		}
		echo $json;
	}
	
	
	
	
	
	
	
	/**
	 * 账户登陆日志
	 * Enter description here ...
	 */
	public function mb_loginlog(){
		$this->display();
	}	
	
	/**
	 * 登录日志列表显示
	 * Enter description here ...
	 */	
	public function ajax_loginlog(){
		$loginlog = D('Loginlog');
		
		$map['uid']=array('eq',$this->user['id']);
    	$count = $loginlog->getCount($map);
    	$limit = $this->page($count,20);
    	$loginlogdata = $loginlog->getdata($map,$limit);
    	
    	$this->assign('loginlogdata',$loginlogdata);
        $this->display();
	}
	
	
	
	
	/**
	 * 地址薄
	 * Enter description here ...
	 */
	public function mb_address(){
		$this->display();
	}
	
	
	/**
	 * 地址薄列表
	 * Enter description here ...
	 */
	public function ajax_address(){
		$address = D('Address');
		$map['uid'] = array('eq',$this->user['id']);
		$n = 1;
		$data  = $address->where($map)->findAll();
		
		$this->assign('data',$data);
		$this->assign('n',$n);
		$this->display();
	}
	
	
	/**
	 * 增加地址薄
	 * Enter description here ...
	 */
	public function mb_address_add(){
		$this->display();
	}
	
	
	/**
	 * 增加地址薄保存
	 * Enter description here ...
	 */
	public function mb_address_insert(){
		$json = null;
		
		$data = Init_GP(array('reciver','phoneno','postcode','address','memo'));
		
		$address = D('Address');
		$data['uid'] = $this->user['id'];
		$data['name'] = $this->user['name'];
		
		$map['uid'] = array('eq',$this->user['id']);
		
		$count = $address->getCount($map);  //获取用户地址薄个数
		if ($count < 3 ){
			$info = $address->insert($data);
			if (GetNum($info)){
				$json = 0;
			}else {
				$json = $info;
			}
		}else {
			$json = '您最多可以输入三个物流信息';
		}
		
		echo $json;
	}
	
	
	
	/**
	 * 编辑地址薄
	 * Enter description here ...
	 */
	public function mb_address_edit(){
		$data = Init_GP(array('id'));  //主键ID
		
		if (GetNum($data['id'])){
			
			$address = D('Address');
			$data = $address->find($data['id']);
			if ($data['uid'] == $this->user['id']){
				$this->assign('data',$data);
			}
		}
		
		$this->display();
	}
	
	/**
	 * 地址簿修改确认
	 * Enter description here ...
	 */
	public function mb_address_update(){
		$data = Init_GP(array('id','reciver','phoneno','postcode','address','memo'));
		
		if (GetNum($data['id'])){
			
			$address = D('Address');
			$addressdata = $address->find($data['id']);
			
			if ($addressdata['uid'] == $this->user['id']){
				$info = $address->update($data);
				
				if (GetNum($info)){
					$json = 0;
				}else {
					$json = $info;
				}
			}
		}
		echo $json;
	}
	
	
	/**
	 * 地址簿删除
	 * Enter description here ...
	 */
	public function mb_address_del(){
		$data = Init_GP(array('id'));  //主键
		if (GetNum($data['id'])){
			$address = D('Address');
			$addressdata = $address->find($data['id']);
			if ($addressdata['uid'] == $this->user['id']){
				$address->delete($data['id']);
			}
		}
	}
	

	
	
	
	
	
		
	/**
	 * 站内短信
	 * Enter description here ...
	 */
	public function mb_message(){
		$this->display();
	}
	
	/**
	 * 站内短信
	 * Enter description here ...
	 */
	public function ajax_message(){
		$data = Init_GP(array('type'));  //站内短信类型
		$type = GetNum($data['type']);
		
		if ($type == 0 || $type == 1){   //系统或个人站内
			$map['type'] = array('eq',$type);
			$map['receptionname'] = array('eq',$this->user['name']);
		}else {  //个人发出
			$map['type'] = array('eq',1);
			$map['sendname'] = array('eq',$this->user['name']);
		}		
		
		
		$letter = M('Letter');   //站内短信
		$letterdata = $letter->where($map)->findAll();
		$letter->where($map)->setField('state',1);  //不是全局广播

		$this->assign('letterdata',$letterdata);
		$this->assign('type',$type);
		$this->display();
	}
	
	
	/**
	 * 删除站内信
	 * Enter description here ...
	 */
	public function deleteselect(){
		$data = Init_GP(array('id'));
		$map['id'] = array('in',$data['id']);
		
		//删除我接收的或者我发送的站内信
		$map['_string'] = "`receptionname` = '{$this->user['name']}' OR `sendname` = '{$this->user['name']}'";
		$letter = M('Letter');
		$info = $letter->where($map)->delete();
		
		if ($info){
			$json = 1; //删除成功
		}else{
			$json = 0; //删除失败
		}
		echo $json;		
	}
	
	
	/**
	 * 发送站内信
	 * Enter description here ...
	 */
	public function send_message(){
		$this->display();
	}
	
	/**
	 * 发送站内信确认
	 * Enter description here ...
	 */
	public function mbsendmsg_insert(){
		$data = Init_GP(array('receptionname','message'));
		
		$config = C('SYSCONFIG');
		$data['sendname'] = $this->user['name'];
		$data['addtime'] = time();
		$data['message'] = preg_replace("#".$config['letter_replacestr']."#i", '***', $data['message']);  //过滤词语
		$data['type'] = 1;
		
		$letter = M('Letter');
		$info = $letter->add($data);
		if ($info){
			$json = 0; //插入成功
		}else {
			$json = '发送失败';
		}
		echo $json;
	}
	
	
	
	
	
	
	
	
	/**
	 * 修改个人信息
	 * Enter description here ...
	 */
	public function mb_modify(){
		$this->display();
	}
	
	
	
	/**
	 * 修改头像确认
	 * Enter description here ...
	 */
	public function mb_modify_logo(){
		$data = Init_GP(array('photo'));
		$head = sub_head($data['photo']);
		
		$user = D('User');
		$map['id'] = array('eq',$this->user['id']);
		$info = $user->where($map)->setField('head',$head);
		
		if (GetNum($info)){
			$json = 0;
		}else {
			$json = $info;
		}
		echo $json;
	}
	
	
	
	/**
	 * 修改基本信息确认
	 * Enter description here ...
	 */	
	public function mb_modify_info(){
		$data = Init_GP(array('realname','sex','birthday'));
		
		$user = D('User');
		$map['id'] = array('eq',$this->user['id']);
		$data['birthday'] = strtotime($data['birthday']);
		$info = $user->where($map)->save($data);
		
		if (GetNum($info)){
			$json = 0;
		}else {
			$json = $info;
		}
		echo $json;
	}
	
	
	
	
	/**
	 * 修改密码确认
	 * Enter description here ...
	 */
	public function mb_modify_pwd(){
		$data = Init_GP(array('oldPassword','userPassword'));
		$oldPassword = md5($data['oldPassword']);
		$userPassword = md5($data['userPassword']);
		
		if ($oldPassword == $this->user['password']) {
			$user = D('User');
			$map['id'] = array('eq',$this->user['id']);
			$info = $user->where($map)->setField('password',$userPassword);
			if (GetNum($info)){
				$json = 0;
			}else {
				$json = $info;
			}
		}else {
			$json = '旧密码错误';
		}
		echo $json;
	}
	
	
	
	/**
	 * 
	 * 更换头像
	 * Enter description here ...
	 */
	public function uploadHead(){
		if(!empty($_FILES['picfile']['size'])){
			$accessory = D('Accessory');
			$file = $accessory->imgUpload('100,50','100,50');
			
			$data = $accessory->find($file);
			
			if(!empty($data)){
				$oldpath = UPLOADIMG.$data['path'];
				$oldthumbnail = UPLOADIMG.$data['thumbnail'];
				$arr = explode('.',$data['path']);
				$ext = $arr[1];
				$filename = time().rand_string(6,1).'.'.$ext;
	
				$newpath = __PUBLIC__.'/images/head/b/'.$filename;
				$newthumbnail = __PUBLIC__.'/images/head/s/'.$filename;
				
				rename($oldpath,$newpath);
				rename($oldthumbnail,$newthumbnail);
				
				$json = $filename;
				
				if(!$this->user['uploadhead']){
					$user = D('User');
					$config = C('SYSCONFIG');
					$userdata = array(
						'id' => $this->user['id'],
						'uploadhead'=>1,
					);
					$user->save($userdata);

					
					
					if(!empty($config['upload_head'])){   //用户上传图像状态：可采用系统头像可上传头像
						$user->setInc('pointb',"id={$this->user['id']}",$config['upload_head']);   //增加虚拟币数量
						
						$transaction = D('Transaction');   //记录交易日志
						$transactiondata = array(
							'uid'=>$this->user['id'],
							'name'=>$this->user['name'],
							'payment'=>'上传头像',
							'remark'=>"用户上传头像奖励{$config['upload_head']}{$config['pointb_name']}点" ,
						
							'money'=>$config['upload_head'],
							'addtime'=>time(),
							'type'=>"pointb",
							'source'=>'7',
							'robot'=>'0'
						
						);
						$transaction->insert($transactiondata);
					}
					
				}
			}else{
				$json = 0;
			}
			echo json_encode($json);
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	/**
	 * 支付统计
	 * 充值/支付记录
	 * Enter description here ...
	 */
	public function mb_countinfo_in(){
		$stime = strtotime(date('Y-m-1',time()));
		$etime = strtotime(date('Y-m-d',time()));
		
		$this->assign('stime',$stime);
		$this->assign('etime',$etime);
		
		$this->display();
	}
	
	
	/**
	 * 支付/充值记录列表检索
	 * Enter description here ...
	 */
	public function ajax_countinfo(){
		$data = Init_GP(array('stime','etime'));
		
		$stime = strtotime($data['stime']);
		$etime = strtotime($data['etime']);

		if (empty($stime) || empty($etime)){
			$stime = strtotime(date('Y-m-1',time()));
			$etime = strtotime(date('Y-m-d H:i:s',time()));
		}
		
		$transaction = D('Transaction');   //查询交易记录
		/*  
		$map = array(
			'uid'=>array('eq',$this->user['id']),
			'addtime'=>array('between',"{$stime},{$etime}")
		);
		*/
		
		
	
		$map = array(
			'addtime'=>array('between',"{$stime},{$etime}")  //添加时间
		);
		
		if($this->user['robot']){  //是机器人   统计赠送给个人的或者全部机器人的或者全部人员的记录
			
			//Log::write(".....是机器人...." ,Log::DEBUG);
			
			$map['robot'] = array(array('eq',2),array('eq',1), 'or') ;
			$map['uid'] = array(array('eq',$this->user['id']),array('eq',0), 'or') ;
			
				
		}else{    //统计赠送给个人的或者全部注册用户的或者全部人员的记录
			
			//Log::write(".....是注册用户...." ,Log::DEBUG);
			$map['robot'] = array(array('eq',2),array('eq',0), 'or') ;
			$map['uid'] = array(array('eq',$this->user['id']),array('eq',0), 'or') ;
						
		}
		
		
		$count = $transaction->getCount($map);
		
		$limit = $this->page($count,20);
		$transactiondata = $transaction->getdata($map,$limit);
		
		
		
		
		$this->assign('transactiondata',$transactiondata);
		$this->display();
	}
	
	
	
	/**
	 * 竞拍统计
	 * Enter description here ...
	 */
	public function mb_countinfo_out(){
		$this->display();
	}
	
	
	/**
	 * 竞拍统计查询列表
	 * Enter description here ...
	 */
	public function ajax_countinfo_out(){
		$auction_log = D('Auction_log');  
		
		$map['uid']=array('eq',$this->user['id']);
    	$count = $auction_log->getCountCount($map);
    	
    	$limit = $this->page($count,20);
    	$auction_logdata = $auction_log->getCountData($map,$limit);
    	
    	$this->assign('auction_logdata',$auction_logdata);
        $this->display();
	}
	
	
	
	/**
	 * 查看详细竞拍日志
	 * Enter description here ...
	 */
	public function biddetail(){
		$data = Init_GP(array('aid'));
		
		$auction_log = D('Auction_log');
		$map['aid']=array('eq',GetNum($data['aid']));
		$map['uid']=array('eq',$this->user['id']);
		
    	$count = $auction_log->getCount($map);
    	
    	$limit = $this->page($count,20,null,'biddetail_list');
    	$auction_logdata = $auction_log->getdata($map,$limit);
    	
    	$this->assign('auction_logdata',$auction_logdata);
    	$this->assign('count',$count);
        $this->display();
	}
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 账户充值
	 * 个人中心管理菜单
	 * Enter description here ...
	 */
	public function mb_count(){
		$this->display();
	}
	
	
	/**
	 * 账户充值
	 * 账户信息
	 * Enter description here ...
	 */
	public function mb_count_single(){
		$payment = D('payment');
		
		//TODO:x现有系统四种在线支付方式
		$alipay = $payment->find(1);  
		//$yeepay = $payment->find(2);
		//$chinabank = $payment->find(3);
		//$tenpay = $payment->find(4);
	
		$this->assign('alipay',$alipay);
		//$this->assign('chinabank',$chinabank);
		//$this->assign('yeepay',$yeepay);
		//$this->assign('tenpay',$tenpay);
		

		$package = D('Package');   
		$packagedata = $package->getdata();
		
		$this->assign('packagedata',$packagedata);
		$this->display();
	}
	
	
	
	/**
	 * 确认支付
	 * Enter description here ...
	 */
	public function ajax_prepay(){
		$data = Init_GP(array('paytype','paypoint_price','oid','pointtype'));
		if ($data['paytype'] == 1){
			$payment = '支付宝';	    	
		}elseif($data['paytype'] == 2){
			$payment = '易宝支付';
		}elseif($data['paytype'] == 3){
			$payment = '网银在线';
		}elseif($data['paytype'] == 4){
			$payment = '财付通';
		}
		
		$id = GetNum($data['oid']);
		if (empty($id)){
			$pid = GetNum($data['pointtype']);
			$package = D('Package');
		    $packagedata = $package->find($pid);
			if (empty($packagedata)){
				$paymentorderdata =array(
					'uid'=>$this->user['id'],
					'name'=>$this->user['name'],
					'payment'=>$payment,
					'amount'=>GetNum($data['paypoint_price'])
				);
			}else {
				$paymentorderdata =array(
					'uid'=>$this->user['id'],
					'name'=>$this->user['name'],
					'payment'=>$payment,
					'amount'=>$packagedata['amount'],
					'pid'=>$packagedata['id'],
				);
			}
			
			$paymentorder = D('Paymentorder');
			$val = $paymentorder->insert($paymentorderdata);
			$type = 'RE';
		}else {
			$order = D('Order');
			$val = $order->find($id);
			$type = 'PA';
		}
		if ($data['paytype'] == 1){
			import("@.ORG.Alipay");
	    	$alipay = new Alipay();
	    	$json = $alipay->_alipayto($val,$type);
		}elseif($data['paytype'] == 2){
			import("@.ORG.Yeepay");
	    	$alipay = new Yeepay();
	    	$json = $alipay->_alipayto($val,$type);
		}elseif($data['paytype'] == 3){
			import("@.ORG.Chinabank");
	    	$alipay = new Chinabank();
	    	$json = $alipay->_alipayto($val,$type);
		}elseif($data['paytype'] == 4){
			import("@.ORG.Tenpay");
	    	$alipay = new Tenpay();
	    	$json = $alipay->_alipayto($val,$type);
		}
		echo $json;
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 竞拍成功
	 * Enter description here ...
	 */
	public function mb_wingame(){	
		$this->display();
	}
	
	/**
	 * 竞拍成功
	 * 未支付与已支付
	 * Enter description here ...
	 */
	public function ajax_wingame(){
		$data = Init_GP(array('type'));
		$type = GetNum($data['type']);
		//统计条件
		$map = array(
			'uid' =>array('eq',$this->user['id']),
			'type' => array('eq',1)
		);
		//数据条件
		$datamap = array(
			'O.uid' =>array('eq',$this->user['id']),
			'O.type' => array('eq',1)
		);
		
		$order = D('Order');
		if ($type == 0){ //未支付订单
			$map['state'] = array('eq',0);
			$datamap['O.state'] = array('eq',0);
			$location = 'ajax_list0';
		}else { //已支付订单
			$map['state'] = array('between','1,4');
			$datamap['O.state'] = array('between','1,4');
			$location = 'ajax_list';
		}
		
		$count = $order->getCount($map);
		$limit = $this->page($count,10,null,$location);
		
		$field = 'O.*,A.sn,A.ordertime,A.realprice,G.goodsfreight,E.number,S.id as sid';
		
		$orderdata = $order->getdata($datamap,$limit,$field);
		
		$this->assign('orderdata',$orderdata);
		$this->assign('type',$type);
		$this->display();
	}
	
	
	
	/**
	 * 保价购买
	 * Enter description here ...
	 */
	public function mb_addgame(){
		$this->display();
	}
	
	/**
	 * 报价购买
	 * 未支付与已支付
	 * Enter description here ...
	 */
	public function ajax_addgame(){
		$data = Init_GP(array('type'));
		$type = GetNum($data['type']);
		//统计条件
		$map = array(
			'uid' =>array('eq',$this->user['id']),
			'type' => array('eq',0)
		);
		//数据条件
		$datamap = array(
			'O.uid' =>array('eq',$this->user['id']),
			'O.type' => array('eq',0)
		);
		$order = D('Order');
		if ($type == 0){
			//未支付订单
			$map['state'] = array('eq',0);
			$datamap['O.state'] = array('eq',0);
			$location = 'ajax_list0';
		}else {
			//已支付订单
			$map['state'] = array('between','1,4');
			$datamap['O.state'] = array('between','1,4');
			$location = 'ajax_list';
		}
		
		$count = $order->getCount($map);
		$limit = $this->page($count,10,null,$location);
		$field = 'O.*,A.sn,A.ordertime,G.goodsfreight,E.number,S.id as sid';
		$orderdata = $order->getdata($datamap,$limit,$field);
		
		
		if ($type == 0){
			$auction_log = D('Auction_log');  //竞拍日志
			$logmap['uid']=array('eq',$this->user['id']);
			
			$config = C('SYSCONFIG');
			foreach ($orderdata as &$value){
				$logmap['aid']=array('eq',$value['aid']);
				$bout = $auction_log->getCount($logmap);
				$auction_logdata = $auction_log->where($logmap)->field('sum(beat) as beatsum')->find();  //用户商品竞拍历史记录
				
				$value['beatnum'] = $auction_logdata['beatsum'];          //实体币总和
				$value['bout'] = $bout;                                      //次数
				$value['premium'] = $value['beatnum'] / $config['rmb_beat']; //换算差价金额
				
				unset($logmap['aid']);
				unset($auction_logdata);
				unset($count);
			}
		}
		
		
		$this->assign('orderdata',$orderdata);
		$this->assign('type',$type);
		$this->display();
	}
	
	
	
	
	/**
	 * 确认返点
	 * Enter description here ...
	 */
	/*
	public function mb_rebeat(){
		$data = Init_GP(array('id'));
		$id = GetNum($data['id']);
		if (empty($id)) {
			$json = '参数错误！';
		}else {
			$order = D('Order');
			$orderdata = $order->find($id);
			
			if (empty($orderdata)){
				$json = '订单不存在！';
			}else {
				if ($orderdata['uid'] != $this->user['id']){
					$json = '非法操作！';
				}else {
					if ($orderdata['isrebeat'] != 1 && $orderdata['state'] != 0){
						$json = '订单状态不正确！';
					}else{
						$map['id'] = array('eq',$id);
						
						$auction = D('Auction');
						$user = D('User');
						$auctiondata = $auction->find($orderdata['aid']);
						
						$uid = $auction->fullRebate($auctiondata);
						
						$order->where($map)->setField('state',5);
						$json = '已返点,操作成功！';
					}
				}
			}
		}
		echo $json;
	}
	*/
	
	
	/**
	 * 失效订单
	 * Enter description here ...
	 */
	public function mb_losegame(){
		$this->display();
	}
	
	/**
	 * 失效订单
	 * Enter description here ...
	 */
	public function ajax_losegame(){
		$order = D('Order');
		$orderdata = $order->getLose($this->user['id']);
		
		$this->assign('orderdata',$orderdata);
		$this->display();
	}
	
	
    /**
     * 确认发货
     * Enter description here ...
     */
	public function mb_clinch(){
		$data = Init_GP(array('id'));
		$id = GetNum($data['id']);
		if (empty($id)) {
			$json = '参数错误！';
		}else {
			$order = D('Order');
			$orderdata = $order->find($id);
			
			if (empty($orderdata)){
				$json = '订单不存在！';
			}else {
				if ($orderdata['uid'] != $this->user['id']){
					$json = '非法操作！';
				}else {
					if ($orderdata['state'] != 2){
						$json = '订单状态不正确！';
					}else{
						$map['id'] = array('eq',$id);
						$order->where($map)->setField('state',3);
						
						$json = '确认发货,操作成功！';
					}
				}
			}
		}
		echo $json;
	}
    
    
	
	/**
	 * 显示支付订单
	 * Enter description here ...
	 */
	public function showOrder(){
		$data = Init_GP(array('id'));
		$id = GetNum($data['id']);
		
		$order = D('Order');
		$map = array(
			'O.id'=>array('eq',$id),
			'O.state'=>array('eq',0),
			'O.uid'=>array('eq',$this->user['id'])
		);
		$field = 'O.*,A.sn,A.ordertime,A.realprice,A.goodsprice,G.goodsfreight,AC.thumbnail';
		$orderdata = $order->getOne($map,$field);
		
		if (!empty($orderdata)){
			//补差价信息
			$auction_log = D('Auction_log');
			$logmap['uid']=array('eq',$this->user['id']);
			$logmap['aid']=array('eq',$orderdata['aid']);
			
			$config = C('SYSCONFIG');
			$bout = $auction_log->getCount($logmap);  //竞标次数
			$auction_logdata = $auction_log->where($logmap)->field('sum(beat) as beatsum')->find();  //实体竞标币
			
			$orderdata['beatnum'] = $auction_logdata['beatsum'];          //闪点总和
			$orderdata['bout'] = $bout;                                      //次数
			$orderdata['premium'] = $orderdata['beatnum'] / $config['rmb_beat']; //换算差价金额
		
		}
		
		$this->assign('orderdata',$orderdata);
		
		$this->display();
	}
	
	
	/**
	 * 订单选择地址薄
	 * Enter description here ...
	 */
	public function userAddress(){
		$address = D('Address');
		$map['uid'] = array('eq',$this->user['id']);
		
		$n = 1;
		$data  = $address->where($map)->findAll();
		
		$this->assign('data',$data);
		$this->assign('n',$n);
		$this->display();
	}
	
	
	/**
	 * 保存订单确认
	 * Enter description here ...
	 */
	public function saveOrder(){
		$data = Init_GP(array('id','userRealName','userPhoneNumber','zipCode','detailedAddress','remarks'));
		$id = GetNum($data['id']);
		
		$order = D('Order');
		$map = array(
			'id'=>array('eq',$id),
			'uid'=>array('eq',$this->user['id']),
			'state'=>array('eq',0)
		);
		
		//查询下是否是本人订单
		$val = $order->where($map)->find();
		
		if (!empty($val)){
			//进行更新
			$tmp = array(
				'id'=>$id,
				'recipient'=>$data['userRealName'],
				'tel'=>$data['userPhoneNumber'],
				'postalcode'=>$data['zipCode'],
				'address'=>$data['detailedAddress'],
				'remark'=>$data['remarks']
			);
			$order->save($tmp);   //保存订单货运信息
			
			
			 
			$ordermap = array(
				'O.id'=>$map['id']
			);
			$field = 'O.*,A.sn,A.ordertime,A.realprice,A.goodsprice,G.goodsfreight,AC.thumbnail';
			$orderdata = $order->getOne($ordermap,$field);
			
			
			//补差价信息
			$auction_log = D('Auction_log');
			$logmap['uid']=$map['uid'];
			$logmap['aid']=array('eq',$orderdata['aid']);
			
			$config = C('SYSCONFIG');
			$bout = $auction_log->getCount($logmap);  //用户竞拍次数
			$auction_logdata = $auction_log->where($logmap)->field('sum(beat) as beatsum')->find();
			$orderdata['beatnum'] = $auction_logdata['beatsum'];          //实体币总和
			$orderdata['bout'] = $bout;                                      //次数
			$orderdata['premium'] = $value['beatnum'] / $config['rmb_beat']; //换算差价金额

		}
		
		
		$this->assign('orderdata',$orderdata);
		
			
		$payment = D('payment');      //支付方式
		$alipay = $payment->find(1);
		$yeepay = $payment->find(2);
		$chinabank = $payment->find(3);
		$tenpay = $payment->find(4);
		
		$this->assign('yeepay',$yeepay);
		$this->assign('alipay',$alipay);
		$this->assign('chinabank',$chinabank);
		$this->assign('tenpay',$tenpay);

		$this->display();
	}
	
    
	
	
}
?>